package com.engineering.elink.util.auth.mail;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import com.engineering.elink.util.ConfigUtil;

public class MailSender {
	private static MailSender mailSender = new MailSender();
	/**
	 * 发送邮件的props文件
	 */
	private final transient Properties props = System.getProperties();
	/**
	 * 邮件服务器登录验证
	 */
	private transient MailAuthenticator authenticator;

	/**
	 * 邮箱session
	 */
	private transient Session session;

	/**
	 * 初始化邮件发送器
	 * 
	 * @param smtpHostName
	 *            SMTP邮件服务器地址
	 * @param username
	 *            发送邮件的用户名(地址)
	 * @param password
	 *            发送邮件的密码
	 */
	private MailSender() {
		init(ConfigUtil.getValue("mail_username"), ConfigUtil.getValue("mail_passwd"), ConfigUtil.getValue("mail_smtpHostName"));
	}

	
	public static MailSender getMailSender () {
		return mailSender;
	}
	
	/**
	 * 初始化
	 * 
	 * @param username
	 *            发送邮件的用户名(地址)
	 * @param password
	 *            密码
	 * @param smtpHostName
	 *            SMTP主机地址
	 */
	private void init(String username, String password, String smtpHostName) {
		// 初始化props
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", smtpHostName);
		props.put("mail.user", username);
		props.put("mail.password", password);
		// 验证
		authenticator = new MailAuthenticator(username, password);
		// 创建session
		session = Session.getInstance(props, authenticator);
	}

	/**
	 * 发送邮件
	 * 
	 * @param recipient
	 *            收件人邮箱地址
	 * @param subject
	 *            邮件主题
	 * @param content
	 *            邮件内容
	 * @throws AddressException
	 * @throws MessagingException
	 */
	public void send(String recipient, String subject, Object content)
			throws AddressException, MessagingException {
		// 创建mime类型邮件
		final MimeMessage message = new MimeMessage(session);
		// 设置发信人
		message.setFrom(new InternetAddress(authenticator.getUsername()));
		// 设置收件人
		message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
		// 设置主题
		message.setSubject(subject);
		// 设置邮件内容
		message.setContent(
		"<section style='width: 100%;background: #fff;'>"+
		"<h1>工程界找回密码邮件</h1>"+
		"<p style='line-height: 24px;font-size: 12px;'>亲爱的"+recipient+"，您好：</p>"+
		"<p style='line-height: 24px;font-size: 12px;'>您使用了邮箱验证方法取回密码，您可以通过点击下面的链接进入重置密码页面：</br><a href='"+content+"'>立即取回密码（该链接在30分钟内有效）</a></p>"+
		"<p style='line-height: 24px;font-size: 12px;'>如果您不能点击上述标签为'立即'的链接，您还可以通过复制（或输入）下面的URL到地址栏中来重置您的密码。</p>"+
		"<p style='line-height: 24px;font-size: 12px;'>"+content+"</p>"+
		"<p style='line-height: 24px;font-size: 12px;'>工程界</p>"+
		"<p style='line-height: 24px;font-size: 12px;'>你的工程界账号是"+recipient+"，此邮件工程界系统发出，请勿回复</p></section>", "text/html;charset=utf-8");
		// 发送
		Transport.send(message);
	}

	/**
	 * 群发邮件
	 * 
	 * @param recipients
	 *            收件人们
	 * @param subject
	 *            主题
	 * @param content
	 *            内容
	 * @throws AddressException
	 * @throws MessagingException
	 */
//	public void send(List<String> recipients, String subject, Object content)
//			throws AddressException, MessagingException {
//		// 创建mime类型邮件
//		final MimeMessage message = new MimeMessage(session);
//		// 设置发信人
//		message.setFrom(new InternetAddress(authenticator.getUsername()));
//		// 设置收件人们
//		final int num = recipients.size();
//		InternetAddress[] addresses = new InternetAddress[num];
//		for (int i = 0; i < num; i++) {
//			addresses[i] = new InternetAddress(recipients.get(i));
//		}
//		message.setRecipients(RecipientType.TO, addresses);
//		// 设置主题
//		message.setSubject(subject);
//		// 设置邮件内容
//		message.setContent(content.toString(), "text/html;charset=utf-8");
//		// 发送
//		Transport.send(message);
//	}

	/**
	 * 发送邮件
	 * 
	 * @param recipient
	 *            收件人邮箱地址
	 * @param mail
	 *            邮件对象
	 * @throws AddressException
	 * @throws MessagingException
	 */
	// public void send(String recipient, SimpleMail content)
	// throws AddressException, MessagingException {
	// send(recipient, mail.getSubject(), mail.getContent());
	// }

	/**
	 * 群发邮件
	 * 
	 * @param recipients
	 *            收件人们
	 * @param mail
	 *            邮件对象
	 * @throws AddressException
	 * @throws MessagingException
	 */
	// public void send(List<String> recipients, SimpleMail mail)
	// throws AddressException, MessagingException {
	// send(recipients, mail.getSubject(), mail.getContent());
	// }
	
	public static void main(String[] args) throws AddressException, MessagingException {
		MailSender.getMailSender().send("hxj.957411398@qq.com", "乂联凌工-忘记密码通过邮箱验证", "http://www.gongchengjie.cn");
//		MailSender.getMailSender().send("hxj.957411398@qq.com", "乂联凌工-忘记密码通过邮箱验证", "<a href='http://www.gongchengjie.cn'>确认！请点击这里验证</a>");
	}

}
